package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.cz.base.tool.DateFormatUtil;
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.converter.BidirectionalConverter;
import ma.glasnost.orika.metadata.Type;

import java.util.Date;

/**
 * @author wangxulu
 * @since 2020.08.14 15:45
 */
public class DatetimeStringConverter extends BidirectionalConverter<Date, String> {

    public static DatetimeStringConverter getInstance() {
        return SingletonEnum.INSTANCE.getInstance();
    }

    @Override
    public String convertTo(Date date, Type<String> type, MappingContext mappingContext) {
        return DateFormatUtil.toString(date, DateFormatUtil.TIME_PATTERN);
    }

    @Override
    public Date convertFrom(String s, Type<Date> type, MappingContext mappingContext) {
        return DateFormatUtil.parseStrToDate(s, DateFormatUtil.TIME_PATTERN);
    }

    enum SingletonEnum {

        //创建一个枚举对象
        INSTANCE;
        private final DatetimeStringConverter datetimeStringConverter;

        /**
         * 私有化枚举的构造函数
         */
        SingletonEnum() {
            datetimeStringConverter = new DatetimeStringConverter();
        }

        public DatetimeStringConverter getInstance() {
            return datetimeStringConverter;
        }
    }
}